//    Copyright (C) 2013  <lugocreation@gmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>
package org.jpgolay.wi.list;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyVetoException;
import javax.swing.*;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import org.jpgolay.wi.persistence.SQLList;

// Referenced classes of package org.jpgolay.wi.list:
//            ListGUI
public class ListController {

    public ListController(String iconName, JDesktopPane desktop) {
        title = "LISTE";
        this.desktop = desktop;
        this.iconName = iconName;
        init();
    }

    public ListController(String iconName, JDesktopPane desktop, String title, String comboBoxModel[], String columnName[]) {
        this.title = "LISTE";
        this.iconName = iconName;
        this.desktop = desktop;
        this.title = title;
        this.comboBoxModel = comboBoxModel;
        this.columnName = columnName;
        show();
    }

    public ListController(String iconName, JDesktopPane desktop, String title, String comboBoxModel[], String columnName[], String request) {
        this.title = "LISTE";
        this.iconName = iconName;
        this.desktop = desktop;
        this.title = title;
        this.comboBoxModel = comboBoxModel;
        this.columnName = columnName;
        loadList(request);
        show();
    }

    protected void init() {
        if (lGui == null || lGui.isClosed()) {
            lGui = null;
            lGui = new ListGUI(iconName);
            initListComponent();
            initListListener();
            lGui.setIconifiable(true);
            lGui.setResizable(true);
            lGui.setMaximizable(true);
            lGui.setClosable(true);
            screen = desktop.getSize();
            lGui.setBounds(screen.width / 2 - 265, screen.height / 2 - 275, 530, 550);
            desktop.add(lGui, JLayeredPane.DEFAULT_LAYER);
        }
    }

    private void initListListener() {
        lGui.getSearchB().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionList();
            }
        });
        lGui.getSearchF().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionList();
            }
        });
    }

    protected void initListComponent() {
        lGui.setTitle(title);
        lGui.setSelectModel(comboBoxModel);
        lGui.setColumnNames(columnName);
    }

    public void show() {
        init();
        if (lGui.isIcon()) {
            try {
                lGui.setIcon(false);
            } catch (PropertyVetoException e) {
                e.printStackTrace();
            }
        }
        if (!lGui.isVisible()) {
            lGui.setVisible(true);
        }
        lGui.toFront();
    }

    public void close() {
        lGui.setVisible(false);
        lGui = null;
        System.gc();
    }

    public void hidden() {
        lGui.setVisible(false);
    }

    public void search() {
        actionList();
    }

    private void actionList() {
        if (requestChoice.length != 0) {
            if (lGui.getSearch().equals("")) {
                if (!loadList(requestChoice[lGui.getSelectIndex()])) {
                    JOptionPane.showMessageDialog(null, requestErrorMsg[lGui.getSelectIndex()]);
                }
            } else if (!loadList(requestLikeChoice[lGui.getSelectIndex()] + lGui.getSearch() + "%'")) {
                JOptionPane.showMessageDialog(null, requestErrorMsg[lGui.getSelectIndex()]);
            }
        }
    }

    private boolean loadList(String request) {
        sqlList = new SQLList();
        if (sqlList.search(request)) {
            data = sqlList.getRowsData();
            for (int i = 0; i < data.length; i++) {
                data[i][0] = new Integer(Integer.parseInt((String) data[i][0]));
            }

            lGui.setData(data);
            if (preferredColumnSize.length != 0) {
                for (int i = 0; i < preferredColumnSize.length; i++) {
                    lGui.getList().getColumnModel().getColumn(i).setPreferredWidth(preferredColumnSize[i]);
                }

            }
            lGui.setInfoRight(data.length + " ligne(s)");
            return true;
        } else {
            return false;
        }
    }

    public boolean showNewList(String request) {
        return loadList(request);
    }

    public String[] getColumnName() {
        return columnName;
    }

    public void setColumnName(String columnName[]) {
        this.columnName = columnName;
    }

    public String[] getComboBoxModel() {
        return comboBoxModel;
    }

    public void setComboBoxModel(String comboBoxModel[]) {
        this.comboBoxModel = comboBoxModel;
    }

    public JDesktopPane getDesktop() {
        return desktop;
    }

    public void setDesktop(JDesktopPane desktop) {
        this.desktop = desktop;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Object[][] getData() {
        return data;
    }

    public void setData(Object data[][]) {
        this.data = data;
    }

    public int[] getPreferredColumnSize() {
        return preferredColumnSize;
    }

    public void setPreferredColumnSize(int preferredColumnSize[]) {
        this.preferredColumnSize = preferredColumnSize;
    }

    public String[] getRequestChoice() {
        return requestChoice;
    }

    public void setRequestChoice(String requestChoice[]) {
        this.requestChoice = requestChoice;
    }

    public String[] getRequestLikeChoice() {
        return requestLikeChoice;
    }

    public void setRequestLikeChoice(String requestLikeChoice[]) {
        this.requestLikeChoice = requestLikeChoice;
    }

    public String[] getRequestErrorMsg() {
        return requestErrorMsg;
    }

    public void setRequestErrorMsg(String requestErrorMsg[]) {
        this.requestErrorMsg = requestErrorMsg;
    }

    public JTable getContenuT() {
        return lGui.getList();
    }
    private Dimension screen;
    private JDesktopPane desktop;
    private String title;
    private String comboBoxModel[] = {
        "", ""
    };
    private String columnName[] = {
        "", "", "", ""
    };
    private String requestChoice[];
    private String requestLikeChoice[];
    private String requestErrorMsg[];
    private Object data[][];
    private int preferredColumnSize[];
    private String iconName;
    private ListGUI lGui;
    private SQLList sqlList;
}
